$(function () {
    function distanceByLnglat(lng1, lat1, lng2, lat2) {
        var radLat1 = Rad(lat1);
        var radLat2 = Rad(lat2);
        var a = radLat1 - radLat2;
        var b = Rad(lng1) - Rad(lng2);
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378137.0;
        s = Math.round(s * 10000) / 10000000;
        s = s.toFixed(2);
        return s;
    }

    function Rad(d) {
        return d * Math.PI / 180.0
    };
    var geolocation = new BMap.Geolocation();
    if (document.location.href.indexOf("/shop/all/") == 0) {
        geolocation = null
    }
    ;
    geolocation.getCurrentPosition(function (r) {
        var _this = this;
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mk = new BMap.Marker(r.point);
            locLng = r.point.lng;
            locLat = r.point.lat;
            $(".morelist").each(function () {
                var ShopLngLat = $(this).find("#showlan").val();
                var InputOF = ShopLngLat.indexOf(",");
                var InputOFLast = ShopLngLat.length;
                var ShopLng = ShopLngLat.slice(0, InputOF);
                var ShopLat = ShopLngLat.slice(InputOF + 1, InputOFLast);
                var dis111 = distanceByLnglat(locLng, locLat, ShopLng, ShopLat);
                $(this).find("#shopspostion").html(dis111 + "km");
            });
            $("#curlat").val(locLat);
            $("#curlng").val(locLng);

            isposition = $("#isposition").val();
            cururl = $("#cururl").val();
            if (isposition == 0) {
                var url = cururl + '&lat=' + locLat + '&lng=' + locLng + '&pos=1';
                window.location = url;
            }
        } else {
            $(".morelist").each(function () {
                $(this).find("#shopspostion").html("无法获取距离" + _this.getStatus() + "");
            });
        }
    }, {
        enableHighAccuracy: true
    });
    //关于状态码
    //BMAP_STATUS_SUCCESS	检索成功。对应数值“0”。
    //BMAP_STATUS_CITY_LIST	城市列表。对应数值“1”。
    //BMAP_STATUS_UNKNOWN_LOCATION	位置结果未知。对应数值“2”。
    //BMAP_STATUS_UNKNOWN_ROUTE	导航结果未知。对应数值“3”。
    //BMAP_STATUS_INVALID_KEY	非法密钥。对应数值“4”。
    //BMAP_STATUS_INVALID_REQUEST	非法请求。对应数值“5”。
    //BMAP_STATUS_PERMISSION_DENIED	没有权限。对应数值“6”。(自 1.1 新增)
    //BMAP_STATUS_SERVICE_UNAVAILABLE	服务不可用。对应数值“7”。(自 1.1 新增)
    //BMAP_STATUS_TIMEOUT	超时。对应数值“8”。(自 1.1 新增)
});


window.qq = window.qq || {}, qq.maps = qq.maps || {}, window.soso || (window.soso = qq), soso.maps || (soso.maps = qq.maps), qq.maps.Geolocation = function () {
    "use strict";
    var e = [], t = null, o = 0, n = "_geoIframe_" + Math.ceil(1e7 * Math.random()), i = document.createElement("iframe"), r = null, s = null, a = null, c = null, u = function (u, p) {
        if (!u)return void alert("key empty");
        if (!p)return void alert("referer  empty");
        var l = document.getElementById(n);
        if (!l) {
            i.setAttribute("id", n);
            var g = "https:";
            i.setAttribute("src", g + "//apis.map.qq.com/tools/geolocation?key=" + u + "&referer=" + p), i.setAttribute("style", "display: none; width: 100%; height: 30%"), document.body ? document.body.appendChild(i) : document.write(i.outerHTML);
            var m = this;
            window.addEventListener("message", function (n) {
                var i = n.data;
                if (i && "geolocation" == i.module) {
                    if (clearTimeout(c), e.length > 0) {
                        var u = e.shift();
                        u.sucCb && u.sucCb(i)
                    }
                    o = 2, m.executeNextGeo(), t && t(i)
                } else {
                    s = (new Date).getTime();
                    var p = s - r;
                    if (p >= a) {
                        if (e.length > 0 && "geo" === e[0].type) {
                            var u = e.shift(), l = {type: "fail", code: 5, message: "The request"};
                            u.errCb && u.errCb(l)
                        }
                        clearTimeout(c), o = -1, m.executeNextGeo()
                    }
                    if (e.length > 0 && "ip" === e[0].type) {
                        var u = e.shift();
                        u.errCb && u.errCb(l)
                    }
                }
            }, !1)
        }
    };
    return u.prototype.executeNextGeo = function () {
        1 !== o && e.length > 0 && (o = 1, e[0].geoprocess())
    }, u.prototype.getLocation = function (t, n, i) {
        if (i && i.timeout) {
            var r = new RegExp("^[0-9]*$");
            if (!r.test(i.timeout))return void alert("timeout error")
        }
        if (e.length > 10)throw new Error("geolocation queue must be lass than 10");
        e.push({
            sucCb: t,
            errCb: n,
            option: i,
            geoprocess: this.getOnceLocation,
            type: "geo"
        }), 1 !== o && (o = 1, this.getOnceLocation())
    }, u.prototype.getOnceLocation = function () {
        var t = e[0] && e[0].option;
        r = (new Date).getTime(), a = t && t.timeout ? +t.timeout : 1e4, clearTimeout(c), c = setTimeout(function () {
            if (e.length > 0) {
                var t = e.shift();
                t.errCb && t.errCb()
            }
        }, a), document.getElementById(n).contentWindow.postMessage("getLocation", "*")
    }, u.prototype.getIpLocation = function (t, n) {
        if (e.length > 10)throw new Error("geolocation queue mast be lass than 10");
        e.push({
            sucCb: t,
            errCb: n,
            geoprocess: this.getOnceIpLocation,
            type: "ip"
        }), 1 !== o && (o = 1, this.getOnceIpLocation())
    }, u.prototype.getOnceIpLocation = function () {
        document.getElementById(n).contentWindow.postMessage("getLocation.robust", "*")
    }, u.prototype.watchPosition = function (e) {
        t = e, document.getElementById(n).contentWindow.postMessage("watchPosition", "*")
    }, u.prototype.clearWatch = function () {
        t = null, document.getElementById(n).contentWindow.postMessage("clearWatch", "*")
    }, u
}();
